﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;

namespace Common
{
    public static class ExtensionHelper
    {
        // Methods
        public static T Convert<T>(this string str, T defaultValue)
        {
            if (string.IsNullOrEmpty(str))
            {
                return defaultValue;
            }
            Type type = typeof(T);
            string fullName = type.FullName;
            if (type.Name == "Nullable`1")
            {
                type = Type.GetType(Regex.Match(fullName, @"((?<=\[)(\w+.\w+)(?=,))").Value);
            }
            MethodInfo info = null;
            foreach (MethodInfo info2 in type.GetMethods())
            {
                if ((info2.Name == "Parse") && (info2.GetParameters().Length == 1))
                {
                    info = info2;
                    break;
                }
            }
            T local = defaultValue;
            try
            {
                if (info != null)
                {
                    local = (T)info.Invoke(null, new string[] { str });
                }
            }
            catch
            {
                return defaultValue;
            }
            return local;
        }

        public static string GetLeft(this string str, int length)
        {
            if (string.IsNullOrEmpty(str))
            {
                return "";
            }
            if (length < str.Length)
            {
                return str.Substring(0, length);
            }
            return str;
        }

        public static string GetRight(this string str, int length)
        {
            if (string.IsNullOrEmpty(str))
            {
                return "";
            }
            if (length < str.Length)
            {
                return str.Substring(str.Length - length);
            }
            return str;
        }

        public static bool ToBoolean(this object obj)
        {
            if ((obj == DBNull.Value) || (obj == null))
            {
                return false;
            }
            if (!(ConvertHelper.GetString(obj).ToLower() == "true"))
            {
                return false;
            }
            return true;
        }

        public static DateTime ToDateTime(object obj)
        {
            DateTime time;
            DateTime.TryParse(ConvertHelper.GetString(obj), out time);
            return time;
        }

        public static string ToDateTimeString(this object obj, string sFormat)
        {
            return ConvertHelper.GetDateTime(obj).ToString(sFormat);
        }

        public static decimal ToDecimal(this object obj)
        {
            return ConvertHelper.GetDecimal(ConvertHelper.GetString(obj));
        }

        public static double ToDouble(this object obj)
        {
            return ConvertHelper.GetDouble(ConvertHelper.GetString(obj));
        }

        public static float ToFloat(this string str)
        {
            int num2;
            if (string.IsNullOrEmpty(str))
            {
                return -1f;
            }
            str = Regex.Replace(str, "[^0-9^-^.]", string.Empty);
            if (str != "")
            {
                str = str.GetLeft(1) + str.Substring(1).Replace('-', '\0');
            }
            int index = str.IndexOf('.');
            if (index == -1)
            {
                return float.Parse(str);
            }
            str = str.Substring(0, index) + "." + str.Substring(index + 1).Replace('.', '\0');
            int.TryParse(str, out num2);
            return (float)num2;
        }

        public static Guid ToGuid(this string str)
        {
            try
            {
                return new Guid(str);
            }
            catch
            {
                return Guid.Empty;
            }
        }

        public static int ToInt(this string str)
        {
            try
            {
                return int.Parse(str);
            }
            catch
            {
                return -1;
            }
        }

        public static int ToInt(this object str, int defaultValue)
        {
            try
            {
                return ConvertHelper.GetInteger(str);
            }
            catch
            {
                return defaultValue;
            }
        }

        public static int ToInt(this string str, int defaultValue)
        {
            try
            {
                return int.Parse(str);
            }
            catch
            {
                return defaultValue;
            }
        }

        public static int? ToIntOrNull(this string str)
        {
            if (str == string.Empty)
            {
                return null;
            }
            return new int?(str.ToInt());
        }

        public static long ToLong(this object obj)
        {
            return ConvertHelper.GetLong(ConvertHelper.GetString(obj));
        }

        public static string ToNumber(this string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return "";
            }
            return Regex.Replace(str, @"\D+", "");
        }

        public static string ToShortDateString(this object obj)
        {
            return ConvertHelper.GetDateTimeString(obj, "yyyy-MM-dd");
        }

        public static short ToShortInt(this object obj)
        {
            return ConvertHelper.GetShortInt(ConvertHelper.GetString(obj));
        }

        public static string UrlDecode(this string str)
        {
            return HttpUtility.UrlDecode(str);
        }

        public static string UrlEncode(this string str)
        {
            return HttpUtility.UrlEncode(str);
        }
    }

}
